﻿using System;
using System.Windows;

namespace Silverlight.InputCtrl
{
    public class LengthValidator: ValidatorBase
    {

        public int MinLength
        {
            get { return (int)GetValue(MinLengthProperty); }
            set { SetValue(MinLengthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MinLength.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MinLengthProperty =
            DependencyProperty.Register("MinLength", typeof(int), typeof(LengthValidator), new PropertyMetadata(int.MinValue));

        public int MaxLength
        {
            get { return (int)GetValue(MaxLengthProperty); }
            set { SetValue(MaxLengthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MinLength.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MaxLengthProperty =
            DependencyProperty.Register("MaxLength", typeof(int), typeof(LengthValidator), new PropertyMetadata(int.MaxValue));

        public override bool Validate()
        {
            base.IsValidResult = true;
            FrameworkElement _elemValidate = base.ElementValidator;
            if (_elemValidate != null && Enabled)
            {
                base.IsValidResult = false;
                string value = "";
                value = ValidatorCommon.GetValueText(_elemValidate,base.PropertyToValidate);
                if (!String.IsNullOrEmpty(value))
                {
                    value = value.Trim();
                    base.IsValidResult = (value.Length >= MinLength && value.Length <= MaxLength);
                }
            }
            return base.IsValidResult;
        }

    }
}
